<script setup>
import { ref } from 'vue'
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { addACoach, deleteACoach, getCoaches, searchACoach, updateACoach } from '@/api/coach';
import { ElMessage, ElMessageBox } from 'element-plus';

// 搜索
const coachSearch = ref('')
// 加载中
const loading = ref(false)
//控制抽屉是否显示
const visibleDrawer = ref(false)
// 控制弹窗显示
const dialogVisible = ref(false)

// 教练数据
const coachData = ref([])
//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(6)//每页条数

// 教练表单模型
const coachModel = ref({
    name: '',
    gender: '',
    phone: '',
    certificate: '',
    specialty: ''
})


// 分页控制
const handleCurrentChange = (val) => {
    pageNum.value = val
    initCoachData()
}

// 修改教练弹窗展示
const showUpdateCoach = (row) => {
    dialogVisible.value = true;
    coachModel.value.name = row.name;
    coachModel.value.gender = row.gender;
    coachModel.value.phone = row.phone;
    coachModel.value.certificate = row.certificate;
    coachModel.value.specialty = row.specialty;
    coachModel.value.id = row.id;
}
// 修改教练
const updateStudent = async () => {
    let coach = await updateACoach(coachModel.value);
    ElMessage.success(coach.data.msg)
    dialogVisible.value = false
    initCoachData()
}

// 添加教练
const addCoach = async () => {
    let result = await addACoach(coachModel.value);
    if (result.data.code == 0) {
        ElMessage.success(result.data.msg)
        visibleDrawer.value = false
    } else {
        ElMessage.error(result.data.msg)
    }
}
// 删除教练
const deleteCoach = async (row) => {
    ElMessageBox.confirm(
        '你确认要删除该教练吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //调用接口
            let result = await deleteACoach(row.id);
            console.log(result);
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            //刷新列表
            initCoachData();
        })
        .catch(() => {
        })

}

// 搜索教练
const searchCoach = async () => {
    let coach = await searchACoach(coachSearch.value);
    if (coach.data.code == 0) {
        coachData.value = coach.data.data.items
        total.value = coach.data.data.total
    }
    else{
        ElMessage.warning(coach.data.msg)
    }
}
// 请求教练数据
const initCoachData = async () => {
    loading.value = true
    let coaches = await getCoaches(pageNum.value);
    coachData.value = coaches.data.data.items[0]
    total.value = coaches.data.total
    loading.value=false
}
initCoachData()
</script>

<template>
    <el-card class="page-container" body-style="padding: 10px 15px;">
        <!-- 头部标题 -->
        <template #header>
            <div class="header">
                <span style="font-weight:800">教练管理</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true;">添加教练</el-button>
                </div>
            </div>
        </template>
        <!-- 中间表格 -->
        <el-table :data="coachData" stripe style="width: 100%" v-loading="loading" height="380" :row-style="{ height: '55px' }">
            <el-table-column label="教练" prop="name"></el-table-column>
            <el-table-column label="性别" prop="gender"></el-table-column>
            <el-table-column label="手机号" prop="phone"> </el-table-column>
            <el-table-column label="资质编号" prop="certificate"></el-table-column>
            <el-table-column label="专长" prop="specialty"></el-table-column>
            <el-table-column width="130">
                <template #header>
                    <el-input v-model="coachSearch" placeholder="输入教练名称" @keyup.enter="searchCoach" />
                </template>
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showUpdateCoach(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteCoach(row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 页码 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" layout="total, prev, pager, next"
            background :total="total" style="margin-top: 20px; justify-content: flex-end"
            @current-change="handleCurrentChange" />
        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" title="添加教练" direction="rtl" size="35%">
            <!-- 添加表单 -->
            <el-form :model="coachModel" label-width="100px">
                <el-form-item label="教练姓名">
                    <el-input v-model="coachModel.name" placeholder="请输入教练姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="coachModel.gender">
                        <el-radio value="男" size="large">男</el-radio>
                        <el-radio value="女" size="large">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="coachModel.phone" placeholder="请输入教练手机号"></el-input>
                </el-form-item>
                <el-form-item label="资质编号">
                    <el-input v-model="coachModel.certificate" placeholder="请输入教练资质编号"></el-input>
                </el-form-item>
                <el-form-item label="专长">
                    <el-select placeholder="请选择" v-model="coachModel.specialty">
                        <el-option value="健身塑形">健身塑形</el-option>
                        <el-option value="瘦身减脂">瘦身减脂</el-option>
                        <el-option value="体操瑜伽">体操瑜伽</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addCoach">提交</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
        <!-- 修改弹窗 -->
        <el-dialog v-model="dialogVisible" title="修改教练" width="30%">
            <!-- 修改表单 -->
            <el-form :model="coachModel" label-width="100px">
                <el-form-item label="教练姓名">
                    <el-input v-model="coachModel.name" placeholder="请输入教练姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="coachModel.gender">
                        <el-radio value="男" size="large">男</el-radio>
                        <el-radio value="女" size="large">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="coachModel.phone" placeholder="请输入教练手机号"></el-input>
                </el-form-item>
                <el-form-item label="资质编号">
                    <el-input v-model="coachModel.certificate" placeholder="请输入教练资质编号"></el-input>
                </el-form-item>
                <el-form-item label="专长">
                    <el-select placeholder="请选择" v-model="coachModel.specialty">
                        <el-option value="健身塑形">健身塑形</el-option>
                        <el-option value="瘦身减脂">瘦身减脂</el-option>
                        <el-option value="体操瑜伽">体操瑜伽</el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="updateStudent">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>

</template>


<style lang="scss" scoped>
/* 主容器 */
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

}

/* 清除浮动 */
.clearfix {
    clear: both;
}
</style>